package zb.blog.base.util;

import org.apache.commons.codec.binary.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @Author: BinZhang
 * @Date: 2018/5/10 21:22
 * @Description:加密和解密
 */
public class EncryptUtil {
    public static final int CIPHERTEXT_LEN = 17;//自定义密文的长度  >=6位数字  需要要17位去接收
    public static final int INTERVAL = 3;//插入字符间隔
    public static final String[] ARR = new String[]{"e", "A", "B", "_", "a", "C", "E", "c", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

    /**
     * @Author: BinZhang
     * @Date: 2018/5/12 10:29
     * @Description:用于用户登录对密码的加密处理
     */
    public static String md5Base64(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] input = str.getBytes();//将传入的字符串转化为byte数组
            byte[] output = md.digest(input);//将字符数组加密
            String new_str = Base64.encodeBase64String(output); //将字符数组转化为字符串
            return new_str;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * BASE64加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(String key) {
        byte[] b = Base64.encodeBase64(key.getBytes(), true);
        String str = new String(b);
        return str;
    }

    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptBASE64(String key) {
        byte[] b1 = Base64.decodeBase64(key);
        return new String(b1);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/16 15:17
     * @Description:自定义数字id简单加密
     */
    public static String customEncrypt(Integer strId) {
        if (strId == null || strId < 0) {
            return null;
        }
        Integer[] index = new Integer[]{1, 2};
        String[] idArr = strId.toString().split("");
        Integer[] idNumArr = new Integer[idArr.length];
        int key = index[(int) (Math.random() * index.length)];
        int centerRand = 0;
        int digitLen = 1;
        if (idArr.length <= INTERVAL * 1) {
            centerRand = 1;
        } else if (idArr.length <= INTERVAL * 2) {
            centerRand = 2;
        } else {
            centerRand = 3;
            digitLen = 2;
        }
        int iL = key, jL = CIPHERTEXT_LEN - 1 - digitLen - (key + 1) - (idArr.length + 1 + centerRand);

        String prefixStr = "";
        String suffixStr = "";
        String center = "";
        for (int i = 0; i < iL; i++) {
            int rand = (int) (Math.random() * ARR.length);
            prefixStr += ARR[rand];
        }
        for (int i = 0; i < jL; i++) {
            int rand = (int) (Math.random() * ARR.length);
            suffixStr += ARR[rand];
        }
        for (int a = 0; a < idArr.length; a++) {
            idNumArr[a] = Integer.valueOf(idArr[a]);
        }
        for (int k = 0; k < idNumArr.length; k++) {
            center += ARR[idNumArr[k]].toString();
        }
        StringBuilder cSb = new StringBuilder(center);
        for (int x = 0; x < centerRand; x++) {
            int y = (x * INTERVAL) + 1;
            cSb.insert(y, ARR[(int) (Math.random() * ARR.length)]);
        }
        int csbLlen = cSb.length();
        int digit = 1;
        if (csbLlen >= 10) {
            digit = 2;
        }
        return prefixStr + csbLlen + cSb + suffixStr + centerRand + key + digit;
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/16 15:22
     * @Description:自定义数字id简单解密
     */
    public static Integer customDecrypt(String ciphertext) {
        if (ciphertext == null || ciphertext.length() != CIPHERTEXT_LEN) {
            return null;
        }
        int digit = Integer.valueOf(ciphertext.substring(ciphertext.length() - 1));
        int key = Integer.valueOf(ciphertext.substring(ciphertext.length() - 2, ciphertext.length() - 1));
        int centerRand = Integer.valueOf(ciphertext.substring(ciphertext.length() - 3, ciphertext.length() - 2));
        int PlaintextLen = Integer.valueOf(ciphertext.substring(key, key + digit));
        String center = ciphertext.substring(key + digit, key + PlaintextLen + digit);
        StringBuffer sbC = new StringBuffer(center);
        for (int i = 0; i < centerRand; i++) {
            sbC.replace(2 * i + 1, 2 * i + 1 + 1, "");
        }
        String Plaintext = "";
        for (int i = 0; i < sbC.length(); i++) {
            String str = String.valueOf(sbC.charAt(i));
            for (int j = 0; j < ARR.length; j++) {
                if (str.equals(ARR[j])) {
                    Plaintext += j;
                    break;
                }
            }
        }
        return Integer.valueOf(Plaintext);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/6/14 0:02
     * @Description:简单的字符串加密和解密
     */
    public static String sim_string_En_De(String str) {
        char[] c = str.toCharArray();
        //使用for循环给字符数组加密
        for (int i = 0; i < c.length; i++) {
            c[i] = (char) (c[i] ^ 20000);
        }
        return new String(c);
    }

    public static void main(String[] args) {
        System.out.println(EncryptUtil.md5Base64("123"));

    }

}
